<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Post Management Library
 *
 * Centralized location to handle post creations/edits including thread titles
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */
class Post_Handler {
	
	var $_CI;
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Post_Handler()
	{
		$this->_CI =& get_instance();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Add a new post
	 *
	 * @access	public
	 * @param	integer	parent id
	 * @param	mixed	view data
	 * @param	bool	new thread
	 */
	function add_post($id, $data, $first = FALSE)
	{		
		$rules['content'] = "trim|required|xss_clean|callback__plugins";
		$fields['content'] = $first ? 'Content' : 'Reply';
		
		if ($first)
		{
			$rules['name'] = "trim|required|xss_clean";
			$fields['name'] = 'Title';
		}
		
		$this->_CI->validation->set_rules($rules);
		$this->_CI->validation->set_fields($fields);

		$this->_CI->validation->set_error_delimiters('<p style="color:#900">', '</p>');
	
		if ($this->_CI->validation->run() == FALSE)
		{
			$data['first'] = $first;
			$this->_CI->parser->parse('global/post', $data);
		}
		else
		{
			$topic_id = $id;
			
			if ($first)
			{
				$this->_CI->load->model('Top_model', 'Topic');
				$id = $this->_CI->Topic->create($this->_CI->input->post('name'), $id);
			}
			
			$this->_CI->load->model('Post_model', 'Post');
			$this->_CI->load->model('User_model', 'User');

			$db_data = array(
				'content' 	=> $this->_CI->input->post('content'),
				'parent_id' => $id,
				'user_id' 	=> $this->_CI->_user['id'],
				'username' 	=> $this->_CI->_user['username'],
				'created'	=> now()
			);
			$new_id = $this->_CI->Post->create($db_data);
		
			$this->_CI->db->update('topics', array('newest_id' => $new_id, 'newest_time' => now()), array('id' => $id));

			$query = $this->_CI->db->get_where('topics', array('id' => $id), 1, 0);
			$topic_data = $query->row_array();
		
			$this->_CI->User->add_post($this->_CI->_user['id']);
		
			$this->_CI->db->update('forums', array('newest_id' => $topic_data['id'], 'newest_time' => $topic_data['newest_time']), array('id' => $topic_data['parent_id']));

			$this->_CI->session->set_flashdata('msg', $data['success_msg']);
			redirect('topic/view/'.$id);
		}
	}

	// --------------------------------------------------------------------
	
	/**
	 * Edit an existing post
	 *
	 * @access	public
	 * @param	integer	post id
	 * @param	bool	first in thread
	 */
	function edit_post($id, $first = FALSE)
	{
		$this->_CI->load->model('Post_model', 'Post');

		$original = $this->_CI->Post->get_post($id);

		if ($this->_CI->_user['id'] !== $original['user_id'] && !$this->_CI->is_admin) {
			$this->_CI->session->set_flashdata('msg', 'You do not have permission to do this.');
			redirect('topic/view/'.$parent_id);
		}

		$content = $this->_CI->plugin->pre_edit($original['content']);
		$data = array(
			'id' 		=> $id,
			'parent_id' => $parent_id,
			'orig' 		=> $content
		);

		$rules['content'] = "trim|required|xss_clean|callback__plugins";
		$this->_CI->validation->set_rules($rules);

		$fields['content'] = 'Reply';
		$this->_CI->validation->set_fields($fields);

		$this->_CI->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->_CI->validation->run() == FALSE)
		{
			$this->_CI->load->view('edit', $data);
		}
		else
		{
			$db_data = array(
				'id'		=> $id,
				'content' 	=> $this->_CI->input->post('content')
			);
			$new_id = $this->_CI->Post->update($db_data);

			$this->_CI->session->set_flashdata('msg', 'Post Updated!');
			redirect('topic/view/'.$parent_id);
		}
	}

	// --------------------------------------------------------------------
}


?>